package day20180131.designMode.Thirteen_Composite;

import java.util.Enumeration;
import java.util.Vector;

/**
 * Created by 14394 on 2018/1/31.
 */
//树
public class TreeNode {
    private String name;
    private TreeNode parent;
    private Vector<TreeNode> children = new Vector<>();

    public TreeNode(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TreeNode getParent(){
        return parent;
    }

    public void setParent(TreeNode parent){
        this.parent = parent;
    }

    public void add(TreeNode node){
        children.add(node);
    }

    public void remove(TreeNode node){
        children.remove(node);
    }

    public Enumeration<TreeNode> getChildren(){
        return children.elements();
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "name='" + name + '\'' +
                ", parent=" + parent +
                ", children=" + children.toString() +
                '}';
    }


    /*使用场景：  将多个对象组合再一起操作，常用于表示树形结构
    *            从一个整体中能独立出部分模块或功能的场景
    * */
    public static void main(String[] args) {
        TreeNode root = new TreeNode("root");
        TreeNode children1 = new TreeNode("children1");
        TreeNode children2 = new TreeNode("children2");
        TreeNode children3 = new TreeNode("children3");
        TreeNode children4 = new TreeNode("children4");
        children1.add(children4);
        root.add(children1);
        root.add(children2);

        System.out.println(root.toString());
    }
}
